package org.srx.myhome;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.srx.struts.form.UserForm;
import org.srx.util.DbAccess;

public class Admin {

	private DbAccess db = null;
	private ResultSet rs = null;
	private Connection conn = null;
	private PreparedStatement ps = null;
	private boolean isOk;

	/**
	 * get userMaxId;
	 * @return
	 */
	public int getUserMaxId() {
		int userMaxId = 0;
		db = new DbAccess();
		String sql = "select max(user_id) from usertable";
		rs = db.getResultSet(sql);
		if (rs != null) {
			try {
				if (rs.next()) {
					userMaxId = rs.getInt(1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if (rs != null) {
						rs.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				db.closeDb();
			}
		}
		return userMaxId + 1;
	}

	/**
	 * 新增用户
	 * @param userForm
	 * @return
	 */
	public boolean insertUserInfo(UserForm userForm) {
		isOk = false;
		db = new DbAccess();
		conn = db.getConn();
		String sql = "insert into usertable values(?,?,?,?)";
		try {
			conn.setAutoCommit(false);
			ps = db.getPreparedStatement(sql);
			int userMaxId = this.getUserMaxId();
			ps.setInt(1, userMaxId);
			ps.setString(2, userForm.getUser_name());
			ps.setString(3, userForm.getPassword());
			ps.setString(4, userForm.getUser_dac());
			ps.execute();
			conn.commit();
			isOk = true;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.rollback();
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			db.closeDb();
		}
		return isOk;
	}
	
	/*
	 * 根据用户名检查一个注册用户名是否存在
	 */
	public boolean isExistOneUser(String user_name){
		boolean isOk =false;
		db = new DbAccess();
		String sql = "select * from usertable where user_name = '"+ user_name +"'";
		rs = db.getResultSet(sql);
		if(rs != null) {
			try {
				if(rs.next()){
					isOk = true;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if(rs != null){
						rs.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				db.closeDb();
			}
		}
		return isOk;
	}
	
	/*
	 * 核对用户的密码
	 */
	public boolean isOkPswd(String user_name, String password){
		boolean isOk =false;
		db = new DbAccess();
		String sql = "select * from usertable where user_name = '"+ user_name +"' and password = '"+ password +"' ";
		rs = db.getResultSet(sql);
		if(rs != null) {
			try {
				if(rs.next()){
					isOk = true;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					if(rs != null){
						rs.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				db.closeDb();
			}
		}
		return isOk;
	}

	/*
	 * 修改用户密码
	 */
	public boolean updatePswd(String user_name, String password) {
		db = new DbAccess();
		String sql = "update usertable set password=? where user_name = ?";
		conn = db.getConn();
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1,password);
			ps.setString(2, user_name);
			if (ps.executeUpdate() >0){
				conn.commit();
				isOk = true;
			} 
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.rollback();
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			}catch (SQLException e) {
					e.printStackTrace();
				}
			}
			db.closeDb();
		return isOk;
	}
	
	/*得到所有的用户信息*/
	public List getAllUserInfo(){
		List userList = new ArrayList();
		UserForm userForm = null;
		db = new DbAccess();
		String sql = "select * from usertable order by user_id";
		rs = db.getResultSet(sql);
		if(rs != null){
			try {
				while(rs.next()){
					userForm = new UserForm();
					userForm.setUser_id(rs.getInt(1));
					userForm.setPassword(rs.getString(2));
					userForm.setUser_name(rs.getString(3));
					userForm.setUser_dac(rs.getString(4));
					userList.add(userForm);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				try {
					if(rs != null){
						rs.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				db.closeDb();
			}
		}
		return userList;
	}
	/*
	 * 删除用户
	 */
	public boolean deleUser(int user_id){
		boolean isOk = false;
		db = new DbAccess();
		String sql = "delete from usertable where user_id = ?";
		conn = db.getConn();
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setInt(1, user_id);
			if (ps.executeUpdate()==1) {
				conn.commit();
				isOk = true;
			} else {
				conn.rollback();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
			if ( ps != null ) {
					ps.close();
			}
			if ( conn != null ) {
					conn.close();
			}
			} catch (SQLException e) {
				e.printStackTrace();
				}
			db.closeDb();
		}
		return isOk;
	}
}
